﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<ThanhCongJSC.Models.Content>>" %>
<%@ Import Namespace="System.Globalization"%>
<%@ Import Namespace="ThanhCongJSC.Helper"%>

<div style="float:right">
    <%= Html.ActionLink("Thêm mới", "AddEditNews", "Admin", new { id = 0, contentType = ViewData["ContentType"] }, new { @class = "button" })%>
    &nbsp;|&nbsp;
    <input id="btnRemoveNews" class="button" type="button" title="Xóa tin tức đã chọn" onclick="removeNews()" value="Xóa"/>
</div>
<div style="clear:both">&nbsp;</div>

<div>
	<form action="" method="post" id="postlist">
        <table border="1px" cellpadding="5" cellspacing="0" width="100%">
            <tr>
                <th width="5%" class="header"><input type="checkbox" id="check-all" name="check-all" /></th>
                <th width="45%" class="header">Tiêu đề</th>
                <th width="15%" class="header">Ngày đăng</th>
                <th width="25%" class="header">Hình ảnh</th>
                <th width="10%" class="header">Thao tác</th>
            </tr>
            <tbody>
                <% foreach (var item in Model)
                   { %>
                    <tr>
                        <td align="center">
                            <%= Html.CheckBox("NewsSelect", false, new { value = item.ContentID, @class = "selectDelete" })%>
                        </td>
                        <td><%= Html.Encode(item.ContentTitle) + " -- " + Html.Encode(item.ContentTitle2)%></td>
                        <td>
                            <%= item.PublishDate.Value.ToString("dd/MM/yyyy", new CultureInfo("vi-Vn")) ?? DateTime.Now.ToString("dd/MM/yyyy", new CultureInfo("vi-Vn"))%>
                        </td>
                        <td align="center">
                            <img alt="hinh anh" src="<%= Url.Content(Constant.PathImageTinTuc + item.ContentImage ?? string.Empty) %>" width="200px" height="80px" />
                        </td>
                        <td>
                            <%= Html.ActionLink("Cập nhật", "AddEditNews", "Admin", new { id = item.ContentID, contentType = ViewData["ContentType"] }, new { @class = "button" })%>
                        </td>
                    </tr>
                <% } %>
            </tbody>
        </table>
    </form>
</div>

<br />

<div>
    <div style="float:right">
    <% 
        int totalpage = Convert.ToInt32(ViewData["TotalPage"]);
        int pageindex = Convert.ToInt32(ViewData["PageIndex"]);
        int pagesize = Convert.ToInt32(ViewData["PageSize"]);
        
            
        int th = pageindex / pagesize;
        int begin = 1;
        int max = totalpage;
        if(th - 1 > 0)
        {
            begin = (th - 1) * pagesize;
        }
        if((th + 1) * pagesize < totalpage)
        {
            max = (th + 1) * pagesize;   
        }
    %>
    <% Response.Write("<input type='button' onclick='loadNewsByPage(" + 1 + ")' value='|<' />");%>
    <%    
        for(int index = begin; index <= max; index++)
        {
            if(index != pageindex)
            {
                if((index == begin && index % pagesize == 0) || (index == max && index % pagesize == 0))
                {
                    %>
                    <% Response.Write("<input type='button' onclick='loadNewsByPage(" + index.ToString() + ")' value='...' />"); %>
                    <%
                }
                else
                {
                    %>
                    <% Response.Write("<input type='button' onclick='loadNewsByPage(" + index.ToString() + ")' value='" + index.ToString() + "' >"); %>
                    <%
                }
            }    
            else
            {
                %>
                <% Response.Write("<input type='button' onclick='loadNewsByPage(" + index.ToString() + ")' value='" + index.ToString() + "' />"); %>
                <%
            }
        }
    %>
	<% Response.Write("<input type='button' class='dung-button' onclick='loadNewsByPage(" + (totalpage).ToString() + ")' value='>|' />");%>
    
    </div>
    <div style="clear:both">&nbsp;</div>
</div>

<script type="text/javascript">

    function loadNewsByPage(pageindex) {
        var status = jQ("#statuslist").val() + "";
        if (status.indexOf("all") == -1) {
            search(pageindex);
        }
        else {
            viewAll(pageindex);
        }
    }
    
    function removeNews() {
        var listid = "";
        jQ("input.selectDelete:checked").each(function () {
            listid += jQ(this).val() + "-";
        });
        
        if (listid.length == 0) {
            alert("Vui lòng chọn một item để xóa");
        }
        else {
            if (confirm("Bạn có chắc muốn xóa không?") == true) {
                jQ.get("/Admin/DeleteContent/" + listid+"?isProject=True",
                    function (data) {
                        loadNewsByPage(<%= ViewData["PageIndex"] %>);
                    });
            }
        }
    }
</script>